#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/8/10 23:03
# @Author : cjw

from httprunner import HttpRunner, Config, Step, RunRequest


class TestLogin(HttpRunner):
    config = (
        Config("幕布测试")
            .verify(False)
            # 设置base_url,只对相对路径生效
            .base_url("https://api2.mubu.com")
            # 定义全局变量，变量的引用$var_name/${var_name}，作用域：当前测试类中
            .variables(**{
                "host": "${get_host()}",
                "origin": "https://mubu.com",
                "phone": "18367157420",
                "password": "qwer128201209q@",
                "data_unique_id": "6e2b8b65-5f01-4930-b0b4-3bfbe8edbb0d",
                'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
            })
            # 导出token变量值，方便其它用例调用
            .export("token")
    )

    teststeps = [
        Step(
            RunRequest("/v3/api/user/phone_login")
                # 定义局部变量，位置固定，作用域：只在当前Step作用域有效
                .with_variables(**{
                "callbackType": 0,
            })
                .post("/v3/api/user/phone_login")
                .with_headers(
                **{
                    "content-length": "69",
                    "sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
                    "sec-ch-ua-mobile": "?0",
                    "user-agent": "$user_agent",
                    "data-unique-id": "$data_unique_id",
                    "content-type": "application/json;charset=UTF-8",
                    "accept": "application/json, text/plain, */*",
                    "jwt-token": "",
                    "x-request-id": "${random_md5()}",
                    "version": "3.0.0-2.0.0.1812",
                    "origin": "$origin",
                    "sec-fetch-site": "same-site",
                    "sec-fetch-mode": "cors",
                    "sec-fetch-dest": "empty",
                    "referer": "$host",
                    "accept-encoding": "gzip, deflate, br",
                    "accept-language": "zh-CN,zh;q=0.9",
                }
            )
                .with_json(
                {
                    "phone": "$phone",
                    "password": "$password",
                    "callbackType": "$callbackType",
                }
            )
                # 参数提取,在响应结果中提取前面需要加body，在响应头中提取需要加headers
                # 参数引用：$var_name/${var_name}
                # .teardown_hook("${sleep(5)}")
                .extract()
                .with_jmespath("body.data.token", "token")
                # .with_jmespath("headers.vary", "vary")
                # 返回值校验
                .validate()
                .assert_equal("status_code", 200)
                .assert_equal("body.code", 0)
        ),
    ]


if __name__ == '__main__':
    TestLogin().test_start()
